﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using MessageLibrary.Messages;

namespace NarutoClient.Pages
{
    /// <summary>
    /// Interaction logic for CharacterPage.xaml
    /// </summary>
    public partial class CharacterPage : UserControl
    {
        MainWindow mainWindow;
        
        public CharacterPage()
        {
            InitializeComponent();
            InitializeListBox();
        }

        public CharacterPage(MainWindow window)
        {
            InitializeComponent();
            InitializeListBox();

            this.mainWindow = window;
        }

        private void InitializeListBox()
        {
            characterListBox.Items.Add("Naruto");
            characterListBox.Items.Add("Sasuke");
            characterListBox.Items.Add("Sakura");
            characterListBox.Items.Add("Minato");
            characterListBox.Items.Add("Hiruzen");
        }

        private void selectButton_Click(object sender, RoutedEventArgs e)
        {
            // zjistime zvolenou postavu
            if (characterListBox.SelectedIndex == -1)
                MessageBox.Show("You must select one of the characters first.");
            else if (characterListBox.SelectedIndex == (int)GamedataLibrary.CharactersID.Naruto)
            {
                mainWindow.MyCharacter = new GamedataLibrary.Characters.Naruto();
                mainWindow.MyCharacterID = GamedataLibrary.CharactersID.Naruto;
            }
            else if (characterListBox.SelectedIndex == (int)GamedataLibrary.CharactersID.Sasuke)
            {
                mainWindow.MyCharacter = new GamedataLibrary.Characters.Sasuke();
                mainWindow.MyCharacterID = GamedataLibrary.CharactersID.Sasuke;
            }
            else if (characterListBox.SelectedIndex == (int)GamedataLibrary.CharactersID.Sakura)
            {
                mainWindow.MyCharacter = new GamedataLibrary.Characters.Sakura();
                mainWindow.MyCharacterID = GamedataLibrary.CharactersID.Sakura;
            }
            else if (characterListBox.SelectedIndex == (int)GamedataLibrary.CharactersID.Minato)
            {
                mainWindow.MyCharacter = new GamedataLibrary.Characters.Minato();
                mainWindow.MyCharacterID = GamedataLibrary.CharactersID.Minato;
            }
            else if (characterListBox.SelectedIndex == (int)GamedataLibrary.CharactersID.Hiruzen)
            {
                mainWindow.MyCharacter = new GamedataLibrary.Characters.Hiruzen();
                mainWindow.MyCharacterID = GamedataLibrary.CharactersID.Hiruzen;
            }

            /// odesleme data serveru
            // vytvori zpravu
            ActionMessage msg = new ActionMessage();
            msg.Action = characterListBox.SelectedIndex;

            try
            {
                // prevede zpravu do binarni podoby
                byte[] buffer = MessageLibrary.MessageComposer.Serialize((int)MessageLibrary.MessageType.Action, msg);
                // odesle buffer na server
                mainWindow.Communication.Send(buffer);
            }
            catch (Exception)
            {
                MessageBox.Show("ERROR");
                mainWindow.Close();
            }

            selectButton.IsEnabled = false;
            waitingTextBlock.Visibility = System.Windows.Visibility.Visible;
            mainWindow.Communication.ReceiveEnemy();

            // prepneme na obrazovku boje
            //mainWindow.BattlePage = new BattlePage(mainWindow);
            //mainWindow.Content = mainWindow.BattlePage;

        }
    }
}
